#include "client.h"
#include "rttmdata.h"
#include "accdata.h"
#include "netter.h"
#include "config.h"

Client::Client() :
    QObject() {
		

}

void Client::start() {
	
	
	connect(&rttmServer, SIGNAL(newConnection(Connection *)), this, SLOT(rttmConnection(Connection *)));
	connect(&accServer, SIGNAL(newConnection(Connection *)), this, SLOT(accConnection(Connection *)));
	
	rttmServer.listen(QHostAddress::Any, RttmServerPort);
	accServer.listen(QHostAddress::Any, AccServerPort);

}

void Client::rttmConnection(Connection *connection) {


	Netter::trace("Client", "nuova connessione Rttm", LogLevelInformation);
	// creo un oggetto di configurazione
	RttmData *data = new RttmData(connection);

	data->name = tr("Prova di nome");
	rttmOperation.lock();
	rttms.append(data);
	rttmOperation.unlock();
	Netter::trace("Client", tr("connessione aggiunta alla collezione...numero:") + 
				QString::number(rttms.count()), LogLevelInformation);
	

}
void Client::accConnection(Connection *connection) {
	
}

void Client::sendRttmMessage(const Message &message) {

	// controllo se sono presenti rttm in ascolto
	if(!rttms.count())
		return;
	
	foreach(RttmData *rttm, rttms) {
		this->sendRttmMessage(message, rttm);
	}

	
}
void Client::sendRttmMessage(const Message &message, const RttmData *connection) {

	

}

void Client::sendMessage(const QString &message) {}
QString Client::nickName() const {}
//bool Client::hasConnection(const QHostAddress &senderIp, int senderPort = -1) const {}





void Client::connectionError(QAbstractSocket::SocketError socketError) {}
void Client::disconnected() {}
void Client::readyForUse() {}


void Client::removeRttmConnection(Connection *connection) {}
void Client::removeAccConnection(Connection *connection) {}